package com.cpht.app.webapp.action;

import java.io.Serializable;

import com.cpht.app.model.Pet;
import com.cpht.app.webapp.action.BasePage;
import com.cpht.app.service.GenericManager;

public class PetForm extends BasePage implements Serializable {
    private GenericManager<Pet, Long> petManager;
    private Pet pet = new Pet();
    private Long id;

    public void setPetManager(GenericManager<Pet, Long> petManager) {
        this.petManager = petManager;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        petManager.remove(pet.getId());
        addMessage("pet.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            pet = petManager.get(id);
        } else {
            pet = new Pet();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pet.getId() == null);
        petManager.save(pet);

        String key = (isNew) ? "pet.added" : "pet.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 